# Maintainer: Alexey Pavlov <alexpux@gmail.com>
# Contributor: Ray Donnelly <mingw.android@gmail.com>
# Contributor: Duong Pham <dthpham@gmail.com>

_realname=numpy
pkgbase=mingw-w64-python-${_realname}
pkgname=("${MINGW_PACKAGE_PREFIX}-python2-${_realname}" "${MINGW_PACKAGE_PREFIX}-python3-${_realname}")
pkgver=1.11.0
pkgrel=1
pkgdesc="Scientific tools for Python (mingw-w64)"
arch=('any')
license=('BSD')
url="http://www.numpy.org/"
makedepends=("${MINGW_PACKAGE_PREFIX}-openblas"
             "${MINGW_PACKAGE_PREFIX}-python3-setuptools"
             "${MINGW_PACKAGE_PREFIX}-python3-nose"
             "${MINGW_PACKAGE_PREFIX}-python2-setuptools"
             "${MINGW_PACKAGE_PREFIX}-python2-nose"
             "${MINGW_PACKAGE_PREFIX}-gcc-fortran")
#options=('!strip' 'debug')
source=(https://downloads.sourceforge.net/numpy/${_realname}-${pkgver}.tar.gz
        0001-detect-mingw-environment.patch
        0002-fix-finding-python2.patch
        0003-gfortran-better-version-check.patch
        0004-fix-testsuite.patch
        0005-mincoming-stack-boundary-32bit-optimized-64bit.patch
        0006-disable-visualcompaq-for-mingw.patch
        0007-disable-64bit-experimental-warning.patch)
sha256sums=('a1d1268d200816bfb9727a7a27b78d8e37ecec2e4d5ebd33eb64e2789e0db43e'
            '94e48602f59a69a9d56886e5b1dab62ffc4fb30b00c5d62d2a818d67d4fd079a'
            'f3864b418791d6f6c1f5a8800c955402934188bdae0614d9e49b1c84f23690d9'
            '77c342fbb13de981d682bdc1889709d9d2507a1566d5ee63004f0b764a62a988'
            '1f9783c356196327a424113e9a5d39c04fc70fef5529fd0d43288ff5274494f4'
            '429d3379e6840dd39763c629488a687a04dc3c4540e58565e75f9809e57f778d'
            '707ce38e179d24a97b6bd01f5f76d0087042c4fbb17eca8e630ad7c6807ada24'
            '00eac381f73172cb03f54fc71a845a3e72a998d78e88cbce752cf52672fc6bd1')

prepare() {
  cd ${_realname}-${pkgver}
  patch -Np1 -i ${srcdir}/0001-detect-mingw-environment.patch
  patch -Np1 -i ${srcdir}/0002-fix-finding-python2.patch
  patch -Np1 -i ${srcdir}/0003-gfortran-better-version-check.patch
  patch -Np1 -i ${srcdir}/0004-fix-testsuite.patch
  # Note, -mincoming-stack-boundary (and the other flags set) doesn't get used except
  # in a test compilation, AFAICT.
  patch -Np1 -i ${srcdir}/0005-mincoming-stack-boundary-32bit-optimized-64bit.patch
  patch -Np1 -i ${srcdir}/0006-disable-visualcompaq-for-mingw.patch
  patch -Np1 -i ${srcdir}/0007-disable-64bit-experimental-warning.patch
  cd ..

  cp -a numpy-${pkgver} ${_realname}-py2-${CARCH}
  cp -a numpy-${pkgver} ${_realname}-py3-${CARCH}

  cd ${_realname}-py2-${CARCH}
  sed -e "s|#![ ]*/usr/bin/python$|#!/usr/bin/python2|" \
      -e "s|#![ ]*/usr/bin/env python$|#!/usr/bin/env python2|" \
      -e "s|#![ ]*/bin/env python$|#!/usr/bin/env python2|" \
      -i $(find . -name '*.py')
}

build() {
  export ATLAS=None
  export LDFLAGS="$LDFLAGS -shared"

  echo "Building Python2"
  cd ${_realname}-py2-${CARCH}
  MSYS2_ARG_CONV_EXCL="--prefix=;--install-scripts=;--install-platlib=" \
    ${MINGW_PREFIX}/bin/python2 setup.py config_fc --fcompiler=gnu95 build

  echo "Building Python3"
  cd ../${_realname}-py3-${CARCH}
  MSYS2_ARG_CONV_EXCL="--prefix=;--install-scripts=;--install-platlib=" \
    ${MINGW_PREFIX}/bin/python3 setup.py config_fc --fcompiler=gnu95 build
}

package_python2-numpy() {
  depends=("${MINGW_PACKAGE_PREFIX}-openblas" "${MINGW_PACKAGE_PREFIX}-python2")
  optdepends=("${MINGW_PACKAGE_PREFIX}-python2-nose: testsuite")

  _pyver=2.7

  export ATLAS=None
  export LDFLAGS="$LDFLAGS -shared"

  cd ${_realname}-py2-${CARCH}
  MSYS2_ARG_CONV_EXCL="--prefix=;--install-scripts=;--install-platlib=" \
    ${MINGW_PREFIX}/bin/python2 setup.py config_fc --fcompiler=gnu95 install --prefix=${MINGW_PREFIX} --root="${pkgdir}" --optimize=1

  install -m755 -d "${pkgdir}${MINGW_PREFIX}/share/licenses/python2-numpy"
  install -m644 LICENSE.txt "${pkgdir}${MINGW_PREFIX}/share/licenses/python2-numpy/"

  install -m755 -d "${pkgdir}${MINGW_PREFIX}/include/python${_pyver}"
  cp -rf ${pkgdir}${MINGW_PREFIX}/lib/python${_pyver}/site-packages/numpy/core/include/numpy "${pkgdir}${MINGW_PREFIX}/include/python${_pyver}/"

  # fix python command in files
  local _mingw_prefix=$(cygpath -am ${MINGW_PREFIX})
  for _f in "${pkgdir}${MINGW_PREFIX}"/bin/*.py; do
    sed -e "s|${_mingw_prefix}/bin/|/usr/bin/env |g" -i ${_f}
  done
}

package_python3-numpy() {
  depends=("${MINGW_PACKAGE_PREFIX}-openblas" "${MINGW_PACKAGE_PREFIX}-python3")
  optdepends=("${MINGW_PACKAGE_PREFIX}-python3-nose: testsuite")

  _pyver=3.5
  _pyinc=${_pyver}m

  export ATLAS=None
  export LDFLAGS="$LDFLAGS -shared"

  cd ${_realname}-py3-${CARCH}
  MSYS2_ARG_CONV_EXCL="--prefix=;--install-scripts=;--install-platlib=" \
    ${MINGW_PREFIX}/bin/python3 setup.py config_fc --fcompiler=gnu95 install --prefix=${MINGW_PREFIX} --root="${pkgdir}" --optimize=1

  install -m755 -d "${pkgdir}${MINGW_PREFIX}/share/licenses/python3-numpy"
  install -m644 LICENSE.txt "${pkgdir}${MINGW_PREFIX}/share/licenses/python3-numpy/"

  install -m755 -d "${pkgdir}${MINGW_PREFIX}/include/python${_pyinc}"
  cp -rf ${pkgdir}${MINGW_PREFIX}/lib/python${_pyver}/site-packages/numpy/core/include/numpy "${pkgdir}${MINGW_PREFIX}/include/python${_pyinc}/"

  # fix python command in files
  local _mingw_prefix=$(cygpath -am ${MINGW_PREFIX})
  for _f in "${pkgdir}${MINGW_PREFIX}"/bin/*.py; do
    sed -e "s|${_mingw_prefix}/bin/|/usr/bin/env |g" -i ${_f}
  done
}

package_mingw-w64-i686-python2-numpy() {
  package_python2-numpy
}

package_mingw-w64-i686-python3-numpy() {
  package_python3-numpy
}

package_mingw-w64-x86_64-python2-numpy() {
  package_python2-numpy
}

package_mingw-w64-x86_64-python3-numpy() {
  package_python3-numpy
}
